

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;

import com.CallbackEvent;
import com.EventRegistration;

public class DeptSearchPanel extends JPanel {
	private JPanel panel;
	private JLabel lblNewLabel;
	private JPanel panel_1;
	private JLabel lblNewLabel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JButton btnNewButton_2;
	private JButton btnNewButton_3;
	private JButton btnNewButton_4;
	private JComboBox comboBox;
	private JTextField textField_10;
	private JButton button;
	private JScrollPane scrollPane;
	private JTable table;
	private int selId=0;
	private String selCon="name";
	private String selected = "";
	DefaultTableModel model;
	private Connection conn;


	private CallbackEvent cbEvent;

	private int index;

	/**
	 * Create the panel.
	 */
	public DeptSearchPanel() {
		setBackground(new Color(173, 216, 230));
		setLayout(new BorderLayout(0, 0));
		add(getPanel(), BorderLayout.NORTH);
		add(getPanel_1(), BorderLayout.WEST);
		add(getPanel_2(), BorderLayout.CENTER);

		btnNewButton_4.setEnabled(false);
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(new Color(245, 255, 250));
			panel.setPreferredSize(new Dimension(10, 34));
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getLabel_1(), BorderLayout.WEST);
			panel.add(getPanel_3(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JLabel getLabel_1() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("부서 조회"); //$NON-NLS-1$
			lblNewLabel.setBackground(new Color(245, 255, 250));
			lblNewLabel.setForeground(new Color(0, 0, 0));
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setFont(new Font("돋움", Font.BOLD, 17));
			lblNewLabel.setPreferredSize(new Dimension(120, 15));
		}
		return lblNewLabel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBackground(new Color(245, 255, 250));
			panel_1.setPreferredSize(new Dimension(120, 10));
			panel_1.setMinimumSize(new Dimension(30, 30));
			panel_1.setLayout(null);
			panel_1.add(getLabel_2());
		}
		return panel_1;
	}
	private JLabel getLabel_2() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("부서의"); //$NON-NLS-1$
			lblNewLabel_1.setFont(new Font("굴림", Font.BOLD, 16));
			lblNewLabel_1.setBounds(50, 10, 58, 26);
		}
		return lblNewLabel_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBackground(new Color(245, 255, 250));
			panel_2.setLayout(null);
			panel_2.add(getComboBox());
			panel_2.add(getTextField_10());
			panel_2.add(getButton());
			panel_2.add(getScrollPane());
		}
		return panel_2;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setBackground(new Color(245, 255, 250));
			panel_3.setLayout(new BorderLayout(0, 0));
			panel_3.add(getBtnNewButton_2(), BorderLayout.EAST);
			panel_3.add(getBtnNewButton_3(), BorderLayout.CENTER);
			panel_3.add(getBtnNewButton_4(), BorderLayout.WEST);
		}
		return panel_3;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton();
			btnNewButton_2.setForeground(new Color(255, 255, 255));
			btnNewButton_2.setBackground(new Color(135, 206, 250));
			btnNewButton_2.setPreferredSize(new Dimension(160, 9));
			btnNewButton_2.setFont(new Font("Dialog", Font.BOLD, 17));
			btnNewButton_2.setText("수정 & 삭제");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					callback("DeptModify");
				}
			});
		}
		return btnNewButton_2;
	}
	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton();
			btnNewButton_3.setForeground(new Color(255, 255, 255));
			btnNewButton_3.setBackground(new Color(135, 206, 250));
			btnNewButton_3.setFont(new Font("Dialog", Font.BOLD, 17));
			btnNewButton_3.setText("부서 등록");
			btnNewButton_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					callback("DeptInsert");
				}
			});
			btnNewButton_3.setMinimumSize(new Dimension(120, 23));
			btnNewButton_3.setMaximumSize(new Dimension(120, 23));
			btnNewButton_3.setPreferredSize(new Dimension(120, 23));
		}
		return btnNewButton_3;
	}
	private JButton getBtnNewButton_4() {
		if (btnNewButton_4 == null) {
			btnNewButton_4 = new JButton(); 
			btnNewButton_4.setForeground(new Color(255, 255, 255));
			btnNewButton_4.setBackground(new Color(135, 206, 250));
			btnNewButton_4.setFont(new Font("Dialog", Font.BOLD, 17));
			btnNewButton_4.setText("부서 조회");
			btnNewButton_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
			btnNewButton_4.setPreferredSize(new Dimension(160, 23));
			btnNewButton_4.setMinimumSize(new Dimension(120, 23));
			btnNewButton_4.setMaximumSize(new Dimension(120, 23));
		}
		return btnNewButton_4;
	}

	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();

			comboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String jcb = comboBox.getSelectedItem().toString();
					
					if (jcb.equals("ID")) 
					{
						selCon = "id";
						System.out.println("ID쓰라");// 작동이 되는지 안되는지 확인을 하기위한 부분
					}
					if (jcb.equals("Name")) 
					{
						selCon = "name";
						System.out.println("이름쓰라"); // 작동이 되는지 안되는지 확인을 하기 위한 부분
					}
				}
			});

			comboBox.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, new Color(70, 130, 180)));
			comboBox.setBackground(UIManager.getColor("Button.light"));

			comboBox.setModel(new DefaultComboBoxModel(new String[] {"ID", "Name"}));
			comboBox.setBounds(12, 10, 88, 27);
		}
		return comboBox;
	}
	
	private JTextField getTextField_10() {
		if (textField_10 == null) {
			textField_10 = new JTextField("");
			textField_10.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) 
					{
						button.doClick();
					}
					
					
				}
			});
			
			textField_10.setColumns(10);
			textField_10.setBounds(112, 10, 273, 27);
		}
		return textField_10;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("Search");

			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					search();
					
					
					
				}
			});

			button.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, new Color(70, 130, 180)));
			button.setBackground(UIManager.getColor("Button.light"));

			button.setBounds(397, 10, 105, 27);
		}
		return button;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 47, 490, 372);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					Object value = table.getValueAt(table.getSelectedRow(), 0);
					
					int selId = (Integer)value;
					
					System.out.println("v mouse : " + selId);

					selectedId(selId);
					
					
					
				}
			});
			
		}
		return table;
	}
	int selectedId(int selId2) 
	{
		selId = selId2;
		
		System.out.println("selid : "+selId);
		return selId;
	}
	
	void search()
	{
		try 
		{
			Vector<MemberBean> vec = new Vector<MemberBean>();
			
		 model = new DefaultTableModel();
					

			connect();
			
			String sql = "select EMPLOYEE_ID,DEPARTMENT_ID,FIRST_NAME,LAST_NAME,MANAGER_ID,EMAIL,PHONE_NUMBER from EMPLOYEES where employee_id like ?";// sql 문장에서 조회를 하기위한 모든 칼럼을 불러온다는 선언을 한다 
			PreparedStatement pstmt = conn.prepareStatement(sql);
			
			pstmt = conn.prepareStatement(sql);  // sql 파일은 conn 형태로 pstmt 에 저장된다 
			
			pstmt.setString(1,"%"+textField_10.getText()+"%"); // connect 로 연결을 하고 First_name에관한 검색을 실행했을 경우 입력한 정보가 해당하는 모든 정보를 표시한다 
			
			if (textField_10.getText().trim().equals("")&&textField_10.getText().trim().equals("")) 
			{
				JOptionPane.showMessageDialog(null, "검색할 이름 또는 ID을 입력해주십시오", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}
			
			ResultSet rs = pstmt.executeQuery();
			

			ResultSetMetaData rsm = rs.getMetaData(); // rsm을 사용하기 위하여 rs.getMetaData의 변수를 지정하고
			
			Vector<String> title = new Vector<>(); // 데이터가 벡터로 들어오게 될때 벡터는 <>안에 String 타입의 값을 저장한다
			Vector data = null; //벡터 데이터는 null로 초기화를 해준다
			
			DefaultTableModel dtm = new DefaultTableModel(); // table에 대한 선언
			
			for(int i = 1 ; i<= rsm.getColumnCount(); i++) {     // 컬럼 카운트는 1부터 시작한다 !   //  columncount는 1부터 시작을 하고 columncount의 가장 마지막까지실행이 된다
				title.add(rsm.getColumnName(i));     // columnName은 초기값 i부터 시작해 반복이 되어 갈수록 더해가며 title로 저장이 된다 
			}
			dtm.setColumnIdentifiers(title);   // table의 데이터가 보여집니다
			table.setModel(dtm);
			
			
			
			while(rs.next()) {     //  위의 for문이 끝나고 그 다음 데이터에 대해 while문으로 시작이 된다
				
				data = new Vector();   //새로운 벡터값을 데이터에 저장을 하고 
				
				for(int i = 1; i <= rsm.getColumnCount(); i++) {   // columncount는 1부터 columncount 의 마지막 까지 실행이된다
					
					System.out.println(i); // 위의 해당하는  for 문의 데이터가 재대로 돌아가는지 확인을 할 수있다
					
					if(rsm.getColumnType(i) == Types.INTEGER) {    // INTEGER 타입의 columncount 가 하나씩 더해가며 data에 저장이 된다 
						data.add(new Integer(rs.getInt(i)));
						System.out.println(rs.getInt(i)); // 위의 해당하는  for 문의 데이터가 재대로 돌아가는지 확인을 할 수있다

					} else {
						data.add(rs.getString(i));    // INTEGER 타입이 아닐경우 string 타입으로 data에 저장이 된다
						System.out.println(rs.getString(i));
					}
				}
				dtm.addRow(data);// 초기값으로 돌아간다
				table.setModel(dtm);
			}	
			
			
			rs.close();          // 로그아웃을 한다 구 봄
			pstmt.close();     // 로그아웃을 한다 구 봄
		}catch (Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	private void connect() throws SQLException, ClassNotFoundException
	{
		conn = DBconn.getConnection();
	}
	
	private void close() throws SQLException
	{
		DBconn.close();
		conn = null;
	}
	
	private void callback( String compare ) {
		EventRegistration er = new EventRegistration(cbEvent);
		er.doWrok(compare);
	}
}

